import Vue from 'vue';
import { getBtn, getAllMenu } from '@/api/permissions'
import { handleGetPermission } from '@/libs/btnsPermission'
import util from '@/libs/util';
const that = Vue.prototype

const state = {
  curMenuBtns: [], // 当前按钮权限菜单
  authorityBtnsList: [],
  menuSiderList: [] // 总的菜单列表
}

const getters = {
  /* 获取系统的按钮 */
  getSystematicBtns: (state, getters) => (param) => {
    let systematicBtns = JSON.parse(sessionStorage.getItem('systemBtnsList'));
    return systematicBtns && systematicBtns.reduce((value, item) => {
      value[that.$md5(item.name)] = that.$md5(param + item.name);
      return value;
    }, {})
  },

  showBtn: (state, getters) => (param) => {
    let visible = false
    let curMenuBtns = state.curMenuBtns.length != 0 ? state.curMenuBtns : sessionStorage.getItem('curMenuBtns')
    visible = handleGetPermission(param, curMenuBtns, JSON.parse(sessionStorage.getItem('systematicBtns')))
    return visible
  },

  /* 获取菜单 */
  getMenuSiderList: (state, getters) => (param) => {
    return state.menuSiderList
  }
}

const mutations = {

  SET_CURBTNS_LIST: (state, data) => {
    let curMenuBtns = []
    data && data.forEach(value => {
      curMenuBtns.push(value.btnParamNumber)
    })
    state.curMenuBtns = curMenuBtns;
    state.authorityBtnsList = data;
    sessionStorage.setItem('curMenuBtns', curMenuBtns)
    sessionStorage.setItem('authorityBtnsList', JSON.stringify(data))
  },

  SET_MENULIST: (state, data) => {
    let menuList = [];
    // 循环一级菜单
    data.forEach(item => {
      // 二级菜单输出列表
      let twoMenu = [];
      let twoChildren = item.childList && item.childList.filter(menuItem => (menuItem.childList && menuItem.childList.length !== 0))
      // 循环二级菜单
      twoChildren && (twoChildren.length != 0) && twoChildren.forEach(twoItem => {
        // 三级菜单的输出列表
        let threeMenu = [];
        // 循环三级菜单找到当前二级菜单的子菜单
        let threeChildren = twoItem.childList
        // 循环三级菜单
        if (threeChildren && threeChildren.length != 0) {
          threeChildren.forEach(threeItem => {
            // push三级菜单项
            threeMenu.push({
              header: 'home',
              icon: threeItem.menuImgUrl,
              path: threeItem.menuUrl,
              title: threeItem.menuName,
              menuId: threeItem.menuId,
              menuName: threeItem.menuName
            })
          })
        }
        // 得到二级菜单项
        twoMenu.push({
          header: 'home',
          icon: twoItem.menuImgUrl,
          path: twoItem.menuUrl,
          title: twoItem.menuName,
          children: threeMenu,
          menuId: twoItem.menuId,
          menuName: twoItem.menuName
        })
      })
      // 得到一级菜单项
      menuList.push({
        header: 'home',
        icon: item.menuImgUrl,
        path: item.menuUrl,
        title: item.menuName,
        checked: item.checked,
        children: twoMenu,
        menuId: item.menuId,
        menuName: item.menuName
      })
    })
    state.menuSiderList = menuList
    util.Pub.setStorageSync('userMenuList', JSON.stringify(menuList))
    /* if (menuList.length == 0) {
      return that.$Message.info('未关联菜单权限')
    } */
    // todo
  }
}

const actions = {
  /* 获取当前页的权限按钮列表 */
  getCurMenuBtnsList ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      getBtn(data).then(res => {
        if (cb) {
          cb(res);
        }
        if (res.code == 0) {
          commit('SET_CURBTNS_LIST', res.data)
        }
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  /* 请求菜单接口 */
  getMenulist: ({ state, commit }, { payload: { data, cb } }) => {
    return new Promise((resolve, reject) => {
      getAllMenu(data).then(res => {
        if (cb) {
          cb(res)
        }
        if (res.code == '0') {
          commit('SET_MENULIST', res.data)
          if (res.data && res.data.length == 0) {
            that.$Message.info('未关联菜单权限')
          }
        }
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}
